package com.example.movie.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.base.app.RouterPath
import com.example.common.entity.MovieVideoEntity
import com.example.common.mvi.BaseMviFragment
import com.example.movie.R
import com.example.movie.adapter.MoviePagingDataAdapter
import com.example.movie.databinding.FragmentMovieListBinding
import com.example.movie.vm.MovieViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

//@AndroidEntryPoint
@Route (path = RouterPath.PATH_FRAGMENT_MOVIE_LIST)
class MovieListFragment : BaseMviFragment<FragmentMovieListBinding>() {
    private val mViewModel: MovieViewModel by activityViewModels()

    @Autowired
    @JvmField
    var tabId: Int = -1

    private lateinit var movieListAdapter: MoviePagingDataAdapter

    override fun getLayoutId(): Int =R.layout.fragment_movie_list

    override fun initViews() {
        ARouter.getInstance().inject(this)

        movieListAdapter = MoviePagingDataAdapter().apply {
            setItemClickListener(object : MoviePagingDataAdapter.MovieItemClickListener{
                override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
                    ARouter.getInstance().build(RouterPath.PATH_PLAY_PAGE)
                        .withInt("moduleType", 1)
                        .withString("itemId",entity.albumid.toString())
                        .withInt("typeId", tabId)
                        .withString("videoPath", entity.videopath)
                        .withString("userId", entity.userid)
                        .navigation()
                }
            })
        }

        mDataBinding.refreshLayout.setOnRefreshListener {
            movieListAdapter.refresh()
        }

        mDataBinding.rvMovie.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = movieListAdapter

            //优化设置
            setHasFixedSize(true)
            setItemViewCacheSize(20)
            isNestedScrollingEnabled = false

            //设置缓存池
            setRecycledViewPool(RecyclerView.RecycledViewPool().apply {
                setMaxRecycledViews(0, 5)
            })

            //关闭动画
            (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false

            //滚动监听
            addOnScrollListener(object : RecyclerView.OnScrollListener(){
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE){
                        Glide.with(this@MovieListFragment).resumeRequests() //恢复图片加载
                    }else{
                        Glide.with(this@MovieListFragment).pauseRequests() //暂定图片加载
                    }
                }

            })
        }

        //加载数据
        loadDataWithPaging3()
    }

    fun loadDataWithPaging3(){
        lifecycleScope.launch {
            if (tabId == -1){
                mViewModel.loadRecommendMovieVideo().collectLatest {
                    if (mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }

                    //设置数据
                    movieListAdapter.submitData(it)

                }
            }else{
                mViewModel.loadMutilMovieVideo(tabId).collectLatest {
                    if (mDataBinding.refreshLayout.isRefreshing){
                        mDataBinding.refreshLayout.finishRefresh()
                    }

                    //设置数据
                    movieListAdapter.submitData(it)
                }
            }
        }
    }



}